{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from anytree import Node,search,RenderTree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=Node('1',bin=0)\n",
    "b=Node('2',bin=1,parent=a)\n",
    "c=Node('3',bin=2,parent=a)\n",
    "d=Node('4',bin=1,parent=a)\n",
    "e=Node('5',bin=3,parent=b)\n",
    "f=Node('6',bin=2,parent=b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Node('/1', bin=0)\n",
      "├── Node('/1/2', bin=1)\n",
      "│   ├── Node('/1/2/5', bin=3)\n",
      "│   └── Node('/1/2/6', bin=2)\n",
      "├── Node('/1/3', bin=2)\n",
      "└── Node('/1/4', bin=1)\n"
     ]
    }
   ],
   "source": [
    "print(RenderTree(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.find_by_attr(a, name='bin',value=5) == None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.random.rand(4,4)\n",
    "[x,y]=np.where(a>0.5)\n",
    "b=np.array(list(zip(x,y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2], dtype=int64)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[np.random.randint(b.shape[0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 9 is out of bounds for axis 0 with size 9",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-46-9f5e901ad96b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mb\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: index 9 is out of bounds for axis 0 with size 9"
     ]
    }
   ],
   "source": [
    "b[9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0], dtype=int64)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "random.choice(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [],
   "source": [
    "table=np.array([\n",
    "    [\n",
    "        [1,1,1],\n",
    "        [0,0,0],\n",
    "        [0,0,0],\n",
    "    ],\n",
    "    [\n",
    "        [0,0,0],\n",
    "        [1,1,1],        \n",
    "        [0,0,0],\n",
    "    ],\n",
    "    [\n",
    "        [0,0,0],\n",
    "        [0,0,0],\n",
    "        [1,1,1],\n",
    "    ],\n",
    "    [\n",
    "        [1,0,0],\n",
    "        [1,0,0],\n",
    "        [1,0,0],\n",
    "    ],\n",
    "    [\n",
    "        [0,1,0],\n",
    "        [0,1,0],\n",
    "        [0,1,0],\n",
    "    ],\n",
    "    [\n",
    "        [0,0,1],\n",
    "        [0,0,1],\n",
    "        [0,0,1],\n",
    "    ],\n",
    "    [\n",
    "        [1,0,0],\n",
    "        [0,1,0],\n",
    "        [0,0,1],\n",
    "    ],\n",
    "    [\n",
    "        [0,0,1],\n",
    "        [0,1,0],\n",
    "        [1,0,0],\n",
    "    ],\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3, 3), (8, 3, 3))"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test=np.array(\n",
    "    [\n",
    "        [1,-1,1],\n",
    "        [1,-1,1],\n",
    "        [-1,1,-1],\n",
    "    ]\n",
    ")\n",
    "#test=test[npaxis,:]\n",
    "test.shape,table.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y=np.where(test==0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(list(zip(x,y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0, 0, 1, 1, 2], dtype=int64), array([0, 2, 0, 2, 1], dtype=int64)]"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x,y]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<zip at 0x121e7a40>"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip((x,y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[]\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,0) == (1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-184-a652fb1877e3>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-184-a652fb1877e3>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    a=[ 1. -3.  2.  1.  0. -1.  0.  0.]\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "a=[ 1. -3.  2.  1.  0. -1.  0.  0.]\n",
    "a.max/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {},
   "outputs": [],
   "source": [
    "from anytree import Node, RenderTree, AsciiStyle, LevelOrderGroupIter,LevelOrderIter\n",
    "f = Node(\"f\")\n",
    "b = Node(\"b\", parent=f,attr=3)\n",
    "a = Node(\"a\", parent=b)\n",
    "d = Node(\"d\", parent=b)\n",
    "c = Node(\"c\", parent=d)\n",
    "e = Node(\"e\", parent=d)\n",
    "g = Node(\"g\", parent=f)\n",
    "i = Node(\"i\", parent=g)\n",
    "h = Node(\"h\", parent=i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[], [Node('/f/b'), Node('/f/g')]]"
      ]
     },
     "execution_count": 247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nodes=[[node for node in children] for children in LevelOrderGroupIter(f, filter_=lambda n: n is not f,maxlevel=2)]\n",
    "nodes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Node('/f')\n",
      "├── Node('/f/b')\n",
      "│   ├── Node('/f/b/a')\n",
      "│   └── Node('/f/b/d')\n",
      "│       ├── Node('/f/b/d/c')\n",
      "│       └── Node('/f/b/d/e')\n",
      "└── Node('/f/g')\n",
      "    └── Node('/f/g/i')\n",
      "        └── Node('/f/g/i/h')\n"
     ]
    }
   ],
   "source": [
    "print(RenderTree(f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "metadata": {},
   "outputs": [],
   "source": [
    "all=[node for node in LevelOrderIter(f, filter_=lambda n: n is not f)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 307,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Node('/f/b', attr=3),\n",
       " Node('/f/g'),\n",
       " Node('/f/b/a'),\n",
       " Node('/f/b/d'),\n",
       " Node('/f/g/i'),\n",
       " Node('/f/b/d/c'),\n",
       " Node('/f/b/d/e'),\n",
       " Node('/f/g/i/h')]"
      ]
     },
     "execution_count": 307,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法1（推荐）：在Python中，False,0,,[],{},()都视为假，因此可以直接进行逻辑运算。此方法效率最高。\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "list_temp = [1,2]\n",
    "\n",
    "print('方法1（推荐）：在Python中，False,0,'',[],{},()都视为假，因此可以直接进行逻辑运算。此方法效率最高。')\n",
    "if list_temp: # 存在值即为真\n",
    "    print(True)\n",
    "else: # list_temp是空的\n",
    "    print(False)\n",
    "    \n",
    "if not []: # 存在值即为真\n",
    "    print(True)\n",
    "else: # list_temp是空的\n",
    "    print(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 4\n"
     ]
    }
   ],
   "source": [
    "print(b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [],
   "source": [
    "find_node=search.find(f,lambda node: node.name == \"b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {},
   "outputs": [],
   "source": [
    "find_node.attr+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "metadata": {},
   "outputs": [],
   "source": [
    "find_node=search.find(f,lambda node: node.name == \"b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_node.attr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1\n",
      "1 2\n",
      "2 3\n",
      "3 4\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3,4]\n",
    "for i,item in enumerate(a):\n",
    "    print(i,item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 302,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = [-10,-20,0,25,3,10,15,-20,25]\n",
    "\n",
    "c=np.array(c)\n",
    "\n",
    "np.argwhere(c==min(c))\n",
    "\n",
    "\n",
    "\n",
    "int(random.choice(np.argwhere(c==min(c))))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xyz 索引位置:  1\n"
     ]
    }
   ],
   "source": [
    "aList = [123, 'xyz', 'runoob', 'abc']\n",
    "\n",
    "print (\"xyz 索引位置: \", aList.index( 'xyz' ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 354,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.00027117228202045975"
      ]
     },
     "execution_count": 354,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(np.pi*0.45*0.45-0.636)/(np.pi*0.45*0.45)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 345,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.2488933152027317e-12"
      ]
     },
     "execution_count": 345,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.00012758374214172363/39269908.16987242"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.2490135624024752e-12"
      ]
     },
     "execution_count": 347,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1963.4954085000002-1963.4954084936207)/1963.4954084936207"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 369,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function rand:\n",
      "\n",
      "rand(...) method of numpy.random.mtrand.RandomState instance\n",
      "    rand(d0, d1, ..., dn)\n",
      "    \n",
      "    Random values in a given shape.\n",
      "    \n",
      "    .. note::\n",
      "        This is a convenience function for users porting code from Matlab,\n",
      "        and wraps `random_sample`. That function takes a\n",
      "        tuple to specify the size of the output, which is consistent with\n",
      "        other NumPy functions like `numpy.zeros` and `numpy.ones`.\n",
      "    \n",
      "    Create an array of the given shape and populate it with\n",
      "    random samples from a uniform distribution\n",
      "    over ``[0, 1)``.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    d0, d1, ..., dn : int, optional\n",
      "        The dimensions of the returned array, must be non-negative.\n",
      "        If no argument is given a single Python float is returned.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray, shape ``(d0, d1, ..., dn)``\n",
      "        Random values.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    random\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.random.rand(3,2)\n",
      "    array([[ 0.14022471,  0.96360618],  #random\n",
      "           [ 0.37601032,  0.25528411],  #random\n",
      "           [ 0.49313049,  0.94909878]]) #random\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.rand)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 370,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.48981205,  0.31404623]])"
      ]
     },
     "execution_count": 370,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-1+2*np.random.rand(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import linalg as LA\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 372,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=-1+2*np.random.rand(1,2)\n",
    "b=LA.norm(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 374,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.98145972 0.99493307]] 1.3975532212497817\n"
     ]
    }
   ],
   "source": [
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 375,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.9531549957621035"
      ]
     },
     "execution_count": 375,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.98145972**2+0.99493307**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 376,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.3975532175062613"
      ]
     },
     "execution_count": 376,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(0.98145972**2+0.99493307**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 427,
   "metadata": {},
   "outputs": [],
   "source": [
    "r=0.45\n",
    "side_len=2*r\n",
    "square_area=side_len**2\n",
    "class area_machine:\n",
    "    def __init__(self,times,r):\n",
    "        self.times=times #随机生成次数\n",
    "        self.len=r #圆形的半径\n",
    "    def isInsideCycle(self,gen):\n",
    "        return LA.norm(gen,axis=1)<self.len\n",
    "    def random_generator(self,times):\n",
    "        return (-1+2*np.random.rand(times,2))*self.len #在正方形中随机生成点\n",
    "    def run(self):\n",
    "        dots=self.random_generator(self.times)\n",
    "        dots_in_cycle=self.isInsideCycle(dots)\n",
    "        dots_in_cycle_n=np.sum(dots_in_cycle)\n",
    "        return (dots_in_cycle_n/self.times)*square_area\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 431,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6360908940000001\n"
     ]
    }
   ],
   "source": [
    "test=area_machine(10000000,r)\n",
    "print(test.run())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 389,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6361725123519332"
      ]
     },
     "execution_count": 389,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.pi*r**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 453,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.5994, 0.6382800000000001, 0.639171, 0.6363117, 0.63647127, 0.636210045]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(2020)\n",
    "times=[100,1000,10000,100000,1000000,10000000]\n",
    "print([area_machine(i,r).run() for i in times])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 402,
   "metadata": {},
   "outputs": [],
   "source": [
    "s=np.random.rand(10000000,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 404,
   "metadata": {},
   "outputs": [],
   "source": [
    "s=np.random.rand(10,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 411,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True, False,  True,  True,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 411,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LA.norm(s,axis=1)>0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 412,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 412,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(LA.norm(s,axis=1)<0.5).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 423,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 423,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((LA.norm(s,axis=1)>0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 418,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True个数： 1\n",
      "False个数： 0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = [True, False, False]\n",
    "print('True个数：',np.sum(arr!=0))\n",
    "print('False个数：',np.sum(arr==0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 454,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.5994, 0.6382800000000001, 0.639171, 0.6363117, 0.63647127, 0.636210045]\n"
     ]
    }
   ],
   "source": [
    "square_area=side_len**2 #正方形面积\n",
    "class area_machine:\n",
    "    def __init__(self,times,r):\n",
    "        self.times=times #随机生成次数\n",
    "        self.len=r #圆形的半径\n",
    "    def isInsideCycle(self,gen):\n",
    "        return LA.norm(gen,axis=1)<self.len\n",
    "    def random_generator(self,times):\n",
    "        return (-1+2*np.random.rand(times,2))*self.len #在正方形中随机生成点\n",
    "    def run(self):\n",
    "        dots=self.random_generator(self.times)\n",
    "        dots_in_cycle=self.isInsideCycle(dots) #查找在圆内的点\n",
    "        dots_in_cycle_n=np.sum(dots_in_cycle)\n",
    "        return (dots_in_cycle_n/self.times)*square_area\n",
    "        \n",
    "times=[100,1000,10000,100000,1000000,10000000]\n",
    "np.random.seed(2020)\n",
    "print([area_machine(i,r).run() for i in times])        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 456,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0033127612513080655"
      ]
     },
     "execution_count": 456,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0.63828-0.6361725123519332)/0.6361725123519332"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(1+x)**3=1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 458,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "99.0"
      ]
     },
     "execution_count": 458,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "100*0.99"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 459,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.555555555555555"
      ]
     },
     "execution_count": 459,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "100/18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 461,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "61.16"
      ]
     },
     "execution_count": 461,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5.56*11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
